package main

import "fmt"

func generateMatrix(n int) [][]int {
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		m[i] = make([]int, n)
	}
	count := 0
	i := -1
	j := -1
	for {
		// 向右
		i++
		j++
		for ; j < n; j++ {
			if m[i][j] != 0 {
				break
			}
			count++
			m[i][j] = count
		}
		// 向下
		j--
		i++
		for ; i < n; i++ {
			if m[i][j] != 0 {
				break
			}
			count++
			m[i][j] = count
		}
		// 向左
		i--
		j--
		for ; j >= 0; j-- {
			if m[i][j] != 0 {
				break
			}
			count++
			m[i][j] = count
		}
		// 向上
		j++
		i--
		for ; i >= 0; i-- {
			if m[i][j] != 0 {
				break
			}
			count++
			m[i][j] = count
		}
		if count == n*n {
			break
		}
	}
	return m
}

func main() {
	ret := generateMatrix(3)
	fmt.Printf("%#v\n", ret)
	ret = generateMatrix(1)
	fmt.Printf("%#v\n", ret)
}
